
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第五节 神经网络的搭建 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter3/" />
    
    
    <link rel="prev" href="section2.4.html" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/section1.1.html">
            
                <a href="../chapter1/section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/section1.2.html">
            
                <a href="../chapter1/section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/section1.3.html">
            
                <a href="../chapter1/section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/section1.4.html">
            
                <a href="../chapter1/section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="section2.1.html">
            
                <a href="section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="section2.2.html">
            
                <a href="section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="section2.3.html">
            
                <a href="section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="section2.4.html">
            
                <a href="section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.5" data-path="section2.5.html">
            
                <a href="section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/section3.1.html">
            
                <a href="../chapter3/section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/section3.2.html">
            
                <a href="../chapter3/section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/section3.3.html">
            
                <a href="../chapter3/section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/section4.1.html">
            
                <a href="../chapter4/section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/section4.2.html">
            
                <a href="../chapter4/section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/section6.2.html">
            
                <a href="../chapter6/section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第五节 神经网络的搭建</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E94;&#x8282;-&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x642D;&#x5EFA;">&#x7B2C;&#x4E94;&#x8282; &#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x642D;&#x5EFA;</h1>
<ul>
<li>&#x524D;&#x5411;&#x4F20;&#x64AD;&#xFF1A;&#x7531;&#x8F93;&#x5165;&#x5230;&#x8F93;&#x51FA;&#xFF0C;&#x642D;&#x5EFA;&#x5B8C;&#x6574;&#x7684;&#x7F51;&#x7EDC;&#x7ED3;&#x6784;</li>
</ul>
<p>&#x63CF;&#x8FF0;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x8FC7;&#x7A0B;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x4E09;&#x4E2A;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code>def forward(x, regularizer):
  w =
  b =
  y =
  return y
</code></pre><p>&#x7B2C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;<code>forward()</code>&#x5B8C;&#x6210;&#x7F51;&#x7EDC;&#x7ED3;&#x6784;&#x7684;&#x8BBE;&#x8BA1;&#xFF0C;&#x4ECE;&#x8F93;&#x5165;&#x5230;&#x8F93;&#x51FA;&#x642D;&#x5EFA;&#x5B8C;&#x6574;&#x7684;&#x7F51;&#x7EDC;&#x7ED3;&#x6784;&#xFF0C;&#x5B9E;&#x73B0;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x53C2;&#x6570;<code>x</code>&#x4E3A;&#x8F93;&#x5165;&#xFF0C;<code>regularizer</code>&#x4E3A;&#x6B63;&#x5219;&#x5316;&#x6743;&#x91CD;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;&#x9884;&#x6D4B;&#x6216;&#x5206;&#x7C7B;&#x7ED3;&#x679C;<code>y</code>&#x3002;</p>
<pre><code>def get_weight(shape, regularizer):
  w = tf.Variable()
  tf.add_to_collection(&apos;losses&apos;, tf.contrib.l2_regularizer(regularizer)(w))
  return w
</code></pre><p>&#x7B2C;&#x4E8C;&#x4E2A;&#x51FD;&#x6570;<code>get_weight()</code>&#x5BF9;&#x53C2;&#x6570;<code>w</code>&#x8BBE;&#x5B9A;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x53C2;&#x6570;<code>shape</code>&#x8868;&#x793A;&#x53C2;&#x6570;<code>w</code>&#x7684;&#x5F62;&#x72B6;&#xFF0C;<code>regularizer</code>&#x8868;&#x793A;&#x6B63;&#x5219;&#x5316;&#x6743;&#x91CD;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;&#x53C2;&#x6570;<code>w</code>&#x3002;&#x5176;&#x4E2D;&#xFF0C;<code>tf.Variable()</code>&#x7ED9;<code>w</code>&#x8D4B;&#x521D;&#x503C;&#xFF0C;<code>tf.add_to_collection()</code>&#x8868;&#x793A;&#x5C06;&#x53C2;&#x6570;<code>w</code>&#x6B63;&#x5219;&#x5316;&#x635F;&#x5931;&#x52A0;&#x5230;&#x603B;&#x635F;&#x5931;<code>losses</code>&#x4E2D;&#x3002;</p>
<pre><code>def get_bias(shape):
  b = tf.Variable()
  return b
</code></pre><p>&#x7B2C;&#x4E09;&#x4E2A;&#x51FD;&#x6570;<code>get_bias()</code>&#x5BF9;&#x53C2;&#x6570;<code>b</code>&#x8FDB;&#x884C;&#x8BBE;&#x5B9A;&#x3002;&#x8BE5;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x53C2;&#x6570;<code>shape</code>&#x8868;&#x793A;&#x53C2;&#x6570;<code>b</code>&#x7684;&#x5F62;&#x72B6;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x4E3A;&#x53C2;&#x6570;<code>b</code>&#x3002;&#x5176;&#x4E2D;&#xFF0C;<code>tf.Variable()</code>&#x8868;&#x793A;&#x7ED9;<code>b</code>&#x8D4B;&#x521D;&#x503C;&#x3002;</p>
<ul>
<li>&#x53CD;&#x5411;&#x4F20;&#x64AD;&#xFF1A;&#x8BAD;&#x7EC3;&#x7F51;&#x7EDC;&#xFF0C;&#x4F18;&#x5316;&#x7F51;&#x7EDC;&#x53C2;&#x6570;&#xFF0C;&#x63D0;&#x9AD8;&#x6A21;&#x578B;&#x51C6;&#x786E;&#x6027;&#x3002;</li>
</ul>
<pre><code>def backward():
  x = tf.placeholder()
  y_ = tf.placeholder()
  y = forward.forward(x, REGULARIZER)
  global_step = tf.Varibale(0, trainable=False)
  loss =
</code></pre><p>&#x51FD;&#x6570;<code>backward()</code>&#x4E2D;&#xFF0C;<code>placeholder()</code>&#x5B9E;&#x73B0;&#x5BF9;&#x6570;&#x636E;&#x96C6;<code>x</code>&#x548C;&#x6807;&#x51C6;&#x7B54;&#x6848;<code>y_</code>&#x5360;&#x4F4D;&#xFF0C;<code>forward.forward()</code>&#x5B9E;&#x73B0;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7684;&#x7F51;&#x7EDC;&#x7ED3;&#x6784;&#xFF0C;&#x53C2;&#x6570;<code>global_step</code>&#x8868;&#x793A;&#x8BAD;&#x7EC3;&#x8F6E;&#x6570;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E3A;&#x4E0D;&#x53EF;&#x8BAD;&#x7EC3;&#x578B;&#x53C2;&#x6570;&#x3002;&#x5728;&#x8BAD;&#x7EC3;&#x7F51;&#x7EDC;&#x6A21;&#x578B;&#x65F6;&#xFF0C;&#x5E38;&#x5C06;&#x6B63;&#x5219;&#x5316;&#x3001;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#x548C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x8FD9;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#x4F5C;&#x4E3A;&#x6A21;&#x578B;&#x4F18;&#x5316;&#x65B9;&#x6CD5;&#x3002;</p>
<ul>
<li>&#x5728;Tensorflow&#x4E2D;&#xFF0C;&#x6B63;&#x5219;&#x5316;&#x8868;&#x793A;&#x4E3A;&#xFF1A;</li>
</ul>
<p>&#x9996;&#x5148;&#xFF0C;&#x8BA1;&#x7B97;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x4E0E;&#x6807;&#x51C6;&#x7B54;&#x6848;&#x7684;&#x635F;&#x5931;&#x503C;</p>
<p>(1) <code>MSE</code>: <code>y</code>&#x4E0E;<code>y_</code>&#x7684;&#x5DEE;&#x8DDD;(loss<em>mse) = `tf.reduce_mean(tf.square(y-y</em>))<code>(2) &#x4EA4;&#x53C9;&#x71B5;:</code>ce = tf.nn.sparse<em>softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y</em>,1))<code>`y</code>&#x4E0E;<code>y_</code>&#x7684;&#x5DEE;&#x8DDD;(cem) = <code>tf.reduce_mean(ce)</code>
(3) &#x81EA;&#x5B9A;&#x4E49;: <code>y</code>&#x4E0E;<code>y_</code>&#x7684;&#x5DEE;&#x8DDD;</p>
<p>&#x5176;&#x6B21;&#xFF0C;&#x603B;&#x635F;&#x5931;&#x503C;&#x4E3A;&#x9884;&#x6D4B;&#x7ED3;&#x679C;&#x4E0E;&#x6807;&#x51C6;&#x7B54;&#x6848;&#x7684;&#x635F;&#x5931;&#x503C;&#x52A0;&#x4E0A;&#x6B63;&#x5219;&#x5316;&#x9879;</p>
<p><code>loss = y &#x4E0E; y_&#x7684;&#x5DEE;&#x8DDD; + tf.add_n(tf.get_collection(&apos;losses&apos;))</code></p>
<ul>
<li>&#x5728;Tensorflow&#x4E2D;&#xFF0C;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#x8868;&#x793A;&#x4E3A;:</li>
</ul>
<pre><code>learning_rate = tf.train.exponential_decay(
    LEARNING_RATE_BASE,
    global_step,
    &#x6570;&#x636E;&#x96C6;&#x603B;&#x6837;&#x672C;&#x6570; / BATCH_SIZE,
    LEARNING_RATE_DECAY,
    staircase=True)
train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
</code></pre><ul>
<li>&#x5728; Tensorflow &#x4E2D;&#xFF0C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x8868;&#x793A;&#x4E3A;:</li>
</ul>
<pre><code>ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([train_step, ema_op]):
  train_op = tf.no_op(name=&apos;train&apos;)
</code></pre><p>&#x5176;&#x4E2D;&#xFF0C;&#x6ED1;&#x52A8;&#x5E73;&#x5747;&#x548C;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#x4E2D;&#x7684; global_step &#x4E3A;&#x540C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x3002;</p>
<ul>
<li>&#x7528; with &#x7ED3;&#x6784;&#x521D;&#x59CB;&#x5316;&#x6240;&#x6709;&#x53C2;&#x6570;</li>
</ul>
<pre><code>with tf.Session() as sess:
  init_op = tf.global_variables_initializer()
  sess.run(init_op)
  for i in range(STEPS):
    sess.run(train_step, feed_dict={x:, y_: })
    if i % &#x8F6E;&#x6570; == 0:
      print
</code></pre><p>&#x5176;&#x4E2D;&#xFF0C;<code>with</code>&#x7ED3;&#x6784;&#x7528;&#x4E8E;&#x521D;&#x59CB;&#x5316;&#x6240;&#x6709;&#x53C2;&#x6570;&#x4FE1;&#x606F;&#x4EE5;&#x53CA;&#x5B9E;&#x73B0;&#x8C03;&#x7528;&#x8BAD;&#x7EC3;&#x8FC7;&#x7A0B;&#xFF0C;&#x5E76;&#x6253;&#x5370;&#x51FA;<code>loss</code>&#x503C;&#x3002;</p>
<ul>
<li>&#x5224;&#x65AD; python &#x8FD0;&#x884C;&#x6587;&#x4EF6;&#x662F;&#x5426;&#x4E3A;&#x4E3B;&#x6587;&#x4EF6;</li>
</ul>
<pre><code>if __name__==&apos;__main__&apos;:
  backward()
</code></pre><p>&#x8BE5;&#x90E8;&#x5206;&#x7528;&#x6765;&#x5224;&#x65AD; python &#x8FD0;&#x884C;&#x7684;&#x6587;&#x4EF6;&#x662F;&#x5426;&#x4E3A;&#x4E3B;&#x6587;&#x4EF6;&#x3002;&#x82E5;&#x662F;&#x4E3B;&#x6587;&#x4EF6;&#xFF0C;&#x5219;&#x6267;&#x884C; backword()&#x51FD;&#x6570;&#x3002;</p>
<p>&#x4F8B;&#x5982;:&#x7528; 300 &#x4E2A;&#x7B26;&#x5408;&#x6B63;&#x6001;&#x5206;&#x5E03;&#x7684;&#x70B9;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>X</mi><mo>[</mo><msub><mi>x</mi><mrow><mn>0</mn></mrow></msub><mo separator="true">,</mo><msub><mi>x</mi><mrow><mn>1</mn></mrow></msub><mo>]</mo></mrow><annotation encoding="application/x-tex">X[x_{0}, x_{1}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.07847em;">X</span><span class="mopen">[</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">0</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mpunct">,</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mclose">]</span></span></span></span>&#x4F5C;&#x4E3A;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x6839;&#x636E;&#x70B9;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>X</mi><mo>[</mo><msub><mi>x</mi><mrow><mn>0</mn></mrow></msub><mo separator="true">,</mo><msub><mi>x</mi><mrow><mn>1</mn></mrow></msub><mo>]</mo></mrow><annotation encoding="application/x-tex">X[x_{0}, x_{1}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.07847em;">X</span><span class="mopen">[</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">0</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mpunct">,</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mclose">]</span></span></span></span>&#x7684;&#x4E0D;&#x540C;&#x8FDB;&#x884C;&#x6807;&#x6CE8;<code>Y_</code>&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x96C6;&#x6807;&#x6CE8;&#x4E3A;&#x7EA2;&#x8272;&#x548C;&#x84DD;&#x8272;&#x3002;&#x6807;&#x6CE8;&#x89C4;&#x5219;&#x4E3A;:&#x5F53;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>x</mi><mrow><mn>0</mn></mrow></msub><mo>+</mo><msub><mi>x</mi><mrow><mn>1</mn></mrow></msub><mo>&lt;</mo><mn>2</mn></mrow><annotation encoding="application/x-tex">x_{0} + x_{1} &lt; 2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">0</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">&lt;</span><span class="mord mathrm">2</span></span></span></span>&#x65F6;&#xFF0C;<code>y_=1</code>&#xFF0C;&#x70B9; <code>X</code>&#x6807;&#x6CE8;&#x4E3A;&#x7EA2;&#x8272;;&#x5F53;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msub><mi>x</mi><mrow><mn>0</mn></mrow></msub><mo>+</mo><msub><mi>x</mi><mrow><mn>1</mn></mrow></msub><mo>&#x2265;</mo><mn>2</mn></mrow><annotation encoding="application/x-tex">x_{0} + x_{1} \geq 2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.79444em;vertical-align:-0.15em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">0</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mbin">+</span><span class="mord"><span class="mord mathit">x</span><span class="msupsub"><span class="vlist"><span style="top:0.15em;margin-right:0.05em;margin-left:0em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathrm mtight">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span><span class="mrel">&#x2265;</span><span class="mord mathrm">2</span></span></span></span>&#x65F6;&#xFF0C;<code>y_=0</code>&#xFF0C;&#x70B9;<code>X</code>&#x6807;&#x6CE8;&#x4E3A;&#x84DD;&#x8272;&#x3002;&#x6211;&#x4EEC;&#x52A0;&#x5165;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#x4F18;&#x5316;&#x6548;&#x7387;&#xFF0C;&#x52A0;&#x5165;&#x6B63;&#x5219;&#x5316;&#x63D0;&#x9AD8;&#x6CDB;&#x5316;&#x6027;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x5316;&#x8BBE;&#x8BA1;&#x65B9;&#x6CD5;&#xFF0C;&#x628A;&#x7EA2;&#x8272;&#x70B9;&#x548C;&#x84DD;&#x8272;&#x70B9;&#x5206;&#x5F00;&#x3002;</p>
<p>&#x4EE3;&#x7801;&#x603B;&#x5171;&#x5206;&#x4E3A;&#x4E09;&#x4E2A;&#x6A21;&#x5757;:&#x751F;&#x6210;&#x6570;&#x636E;&#x96C6;(generateds.py)&#x3001;&#x524D;&#x5411;&#x4F20;&#x64AD;(forward.py)&#x3001;&#x53CD;&#x5411;&#x4F20;&#x64AD; (backward.py)&#x3002;</p>
<p>(1)&#x751F;&#x6210;&#x6570;&#x636E;&#x96C6;&#x7684;&#x6A21;&#x5757;(generateds.py)</p>
<p>(2)&#x524D;&#x5411;&#x4F20;&#x64AD;&#x6A21;&#x5757;(forward.py)</p>
<pre><code>#coding:utf-8
#0&#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF0C;&#x751F;&#x6210;&#x6A21;&#x62DF;&#x6570;&#x636E;&#x96C6;
import tensorflow as tf

#&#x5B9A;&#x4E49;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x8F93;&#x5165;&#x3001;&#x53C2;&#x6570;&#x548C;&#x8F93;&#x51FA;&#xFF0C;&#x5B9A;&#x4E49;&#x524D;&#x5411;&#x4F20;&#x64AD;&#x8FC7;&#x7A0B;
def get_weight(shape, regularizer):
  w = tf.Variable(tf.random_normal(shape), dtype=tf.float32)
  tf.add_to_collection(&apos;losses&apos;,tf.contrib.layers.l2_regularizer(regularizer)(w))
  return w

def get_bias(shape):
  b = tf.Variable(tf.constant(0.01, shape=shape))
  return b

def forward(x, regularizer):
  w1 = get_weight([2,11], regularizer)
  b1 = get_bias([11])
  y1 = tf.nn.relu(tf.matmul(x, w1) + b1)

  w2 = get_weight([11,1], regularizer)
  b2 = get_bias([1])
  y = tf.matmul(y1, w2) + b2 #&#x8F93;&#x51FA;&#x5C42;&#x4E0D;&#x8FC7;&#x6FC0;&#x6D3B;

  return y
</code></pre><p>(3) &#x53CD;&#x5411;&#x4F20;&#x64AD;&#x6A21;&#x5757;(backward.py)</p>
<pre><code>#coding:utf-8
#0&#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF0C;&#x751F;&#x6210;&#x6A21;&#x62DF;&#x6570;&#x636E;&#x96C6;
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import generateds
import forward

STEPS = 40000
BATCH_SIZE = 30
LEARNING_RATE_BASE = 0.001
LEARNING_RATE_DECAY = 0.999
REGULARIZER = 0.01

def backward():
  x = tf.placeholder(tf.float32, shape=(None,2))
  y_ = tf.placeholder(tf.float32, shape=(None,1))

  X, Y_, Y_c = generateds.generateds()
  y = forward.forward(x, REGULARIZER)
  global_step = tf.Variable(0, trainable=False)
  learning_rate = tf.train.exponential_decay(
    LEARNING_RATE_BASE,
    global_step,
    300/BATCH_SIZE,
    LEARNING_RATE_DACAY,
    staircase=True)

  # &#x5B9A;&#x4E49;&#x635F;&#x5931;&#x51FD;&#x6570;
  loss_mse = tf.reduce_mean(tf.square(y-y_))
  loss_total = loss_mse + tf.add_n(tf.get_collection(&apos;losses&apos;))

  # &#x5B9A;&#x4E49;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x65B9;&#x6CD5;&#xFF1A;&#x5305;&#x542B;&#x6B63;&#x5219;&#x5316;
  train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total)
  with tf.Session() as sess:
    init_op = tf.global_varibales_initailizer()
    sess.run(init_op)
    for i in range(STEPS):
      start = (i*BATCH_SIZE) % 300
      end = start + BATCH_SIZE
      sess.run(train_step, feed_dict={x:X[start:end], y_:Y_[start:end]})
      if i % 2000 == 0:
        loss_v = sess.run(loss_total, feed_dict={x:X,y_:Y})
        print(&quot;After %d steps, loss is: %f&quot; % (i, loss_v))
    xx, yy = np.mgrid[-3:3:.01, -3:3:.01]
    grid = np.c_[xx.ravel(), yy.ravel()]
    probs = sess.run(y, feed_dict={x:grid})
    probs = probs.reshape(xx.shape)

  plt.scatter(X[:0], X[:,1], c=np.squeeze(Y_c))
  plt.contour(xx, yy, probs, levels=[.5])
  plt.show()

if __name__ == &apos;__main__&apos;:
  backward()
</code></pre><p>&#x8FD0;&#x884C;&#x4EE3;&#x7801;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-07-29-15328793451699.jpg" width="400"></p>
<p>&#x7531;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x53EF;&#x89C1;&#xFF0C;&#x7A0B;&#x5E8F;&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x5316;&#x8BBE;&#x8BA1;&#x65B9;&#x6CD5;&#xFF0C;&#x52A0;&#x5165;&#x6307;&#x6570;&#x8870;&#x51CF;&#x5B66;&#x4E60;&#x7387;&#xFF0C;&#x4F7F;&#x7528;&#x6B63;&#x5219;&#x5316;&#x540E;&#xFF0C;&#x7EA2;&#x8272;&#x70B9;&#x548C;&#x84DD;&#x8272;&#x70B9; &#x7684;&#x5206;&#x5272;&#x66F2;&#x7EBF;&#x76F8;&#x5BF9;&#x5E73;&#x6ED1;&#xFF0C;&#x6548;&#x679C;&#x53D8;&#x597D;&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-08-18 15:53:44
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="section2.4.html" class="navigation navigation-prev " aria-label="Previous page: 第四节 正则化">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter3/" class="navigation navigation-next " aria-label="Next page: 第三章 全连接网络基础">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第五节 神经网络的搭建","level":"1.3.5","depth":2,"next":{"title":"第三章 全连接网络基础","level":"1.4","depth":1,"path":"chapter3/README.md","ref":"chapter3/README.md","articles":[{"title":"第一节 MINIST数据","level":"1.4.1","depth":2,"path":"chapter3/section3.1.md","ref":"chapter3/section3.1.md","articles":[]},{"title":"第二节 模块化搭建神经网络方法","level":"1.4.2","depth":2,"path":"chapter3/section3.2.md","ref":"chapter3/section3.2.md","articles":[]},{"title":"第三节 手写数字识别准确率输出","level":"1.4.3","depth":2,"path":"chapter3/section3.3.md","ref":"chapter3/section3.3.md","articles":[]}]},"previous":{"title":"第四节 正则化","level":"1.3.4","depth":2,"path":"chapter2/section2.4.md","ref":"chapter2/section2.4.md","articles":[]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter2/section2.5.md","mtime":"2018-08-18T07:53:44.136Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

